package com.cloudbroker.bcs.platform.dav.middle.bizservice.impl;

import com.cloudbroker.bcs.common.entity.HSBCPSBackVO;
import com.cloudbroker.bcs.common.util.VOUtil;
import com.cloudbroker.bcs.platform.dav.bizapi.constants.HSBCPSErrorCodes;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.wechat.WechatShareBizService;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.wechat.result.WechatShareDetailResult;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.wechat.vo.WechatShareForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.wechat.vo.WechatShareGetForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.wechat.vo.WechatShareIdForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.wechat.vo.WechatShareUpdateForm;
import com.cloudbroker.bcs.platform.dav.middle.context.HSBCPSBizDataContext;
import com.cloudbroker.bcs.platform.dav.middle.entity.WechatShare;
import com.cloudbroker.bcs.platform.dav.middle.service.WechatShareService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * Created by zhaojp18008 on 2016/5/19.
 */
@Service
public class WechatShareBizServiceImpl implements WechatShareBizService {
    @Resource
    private WechatShareService wechatShareService;
    
    @Override
    public WechatShareDetailResult funcWechatShareDetailGet(WechatShareGetForm param) {
        Long broker_id = HSBCPSBizDataContext.getBrokerId();
        if (null == broker_id) {
            return VOUtil.genErrorResult(HSBCPSErrorCodes.ClientErr.NO_SUCH_CLIENT, WechatShareDetailResult.class);
        }

        String wechatshare_id = param.getWechatshare_id();
        WechatShare wechatShare;
        if (null == wechatshare_id || wechatshare_id.isEmpty()) {
            wechatShare = wechatShareService.getWechatShareByBrokerId(broker_id);
        } else {
            wechatShare = wechatShareService.get(wechatshare_id);
        }
        if (wechatShare != null && !broker_id.equals(wechatShare.getBroker_id())) {
            return VOUtil.genErrorResult(HSBCPSErrorCodes.ClientErr.EXCEED_POWER, WechatShareDetailResult.class);
        }
        if (wechatShare != null) {
            WechatShareDetailResult wechatShareDetailResult = new WechatShareDetailResult();
            wechatShareDetailResult.setBroker_id(wechatShare.getBroker_id());
            wechatShareDetailResult.setWechatshare_id(wechatShare.getWechatshare_id());
            wechatShareDetailResult.setCreate_date(wechatShare.getCreate_time());
            wechatShareDetailResult.setImg_url(wechatShare.getImg_url());
            wechatShareDetailResult.setLink_url(wechatShare.getLink_url());
            wechatShareDetailResult.setShare_abstract(wechatShare.getShare_abstract());
            wechatShareDetailResult.setShare_times(wechatShare.getShare_times());
            wechatShareDetailResult.setShare_title(wechatShare.getShare_title());
            return VOUtil.setSuccessResult(wechatShareDetailResult);
        } else {
            
            return VOUtil.genErrorResult(HSBCPSErrorCodes.WechatShareErr.NO_SUCH_WECHAT_SHARE,
                    WechatShareDetailResult.class);
        }
    }
    
    @Override
    public HSBCPSBackVO funcWechatShareSet(WechatShareForm param) {
        Long broker_id = HSBCPSBizDataContext.getBrokerId();
        if (null == broker_id) {
            return VOUtil.genErrorResult(HSBCPSErrorCodes.ClientErr.NO_SUCH_CLIENT, WechatShareDetailResult.class);
        }
        
        WechatShare wechatShare = wechatShareService.getWechatShareByBrokerId(broker_id);
        
        if (wechatShare == null) {
            wechatShare = new WechatShare();
            wechatShare.setShare_abstract(param.getShare_abstract());
            wechatShare.setBroker_id(broker_id);
            wechatShare.setLink_url(param.getLink_url());
            wechatShare.setImg_url(param.getImg_url());
            wechatShare.setQrcode_url(param.getQrcode_url());
            wechatShare.setShare_title(param.getShare_title());
            /*分享次数初始化为0*/
            wechatShare.setShare_times(0);
            wechatShare.setCreate_time(new Date());
            
            wechatShareService.save(wechatShare);
        } else {
            WechatShareUpdateForm form = new WechatShareUpdateForm();
            form.setWechatshare_id(String.valueOf(wechatShare.getWechatshare_id()));
            form.setShare_abstract(param.getShare_abstract());
            form.setShare_title(param.getShare_title());
            form.setLink_url(param.getLink_url());
            form.setImg_url(param.getImg_url());
            form.setQrcode_url(param.getQrcode_url());
            
            updateWechatShare(form, broker_id);
        }
        
        return VOUtil.genSuccessResultVo();
    }
    
    @Override
    public HSBCPSBackVO funcWechatShareUpdate(WechatShareUpdateForm param) {
        Long broker_id = HSBCPSBizDataContext.getBrokerId();
        if (null == broker_id) {
            return VOUtil.genErrorResult(HSBCPSErrorCodes.ClientErr.NO_SUCH_CLIENT, HSBCPSBackVO.class);
        }
        
        WechatShare wechatShare = wechatShareService.get(param.getWechatshare_id());
        if (wechatShare == null) {
            return VOUtil.genErrorResult(HSBCPSErrorCodes.WechatShareErr.NO_SUCH_WECHAT_SHARE, HSBCPSBackVO.class);
        }
        if (!broker_id.equals(wechatShare.getBroker_id())) {
            return VOUtil.genErrorResult(HSBCPSErrorCodes.ClientErr.EXCEED_POWER, HSBCPSBackVO.class);
        }
        updateWechatShare(param, broker_id);
        return VOUtil.genSuccessResultVo();
    }
    
    @Override
    public HSBCPSBackVO funcWechatShareDel(WechatShareIdForm param) {
        Long broker_id = HSBCPSBizDataContext.getBrokerId();
        if (null == broker_id) {
            return VOUtil.genErrorResult(HSBCPSErrorCodes.ClientErr.NO_SUCH_CLIENT, HSBCPSBackVO.class);
        }
        
        WechatShare wechatShare = wechatShareService.get(param.getWechatshare_id());
        if (wechatShare == null || !broker_id.equals(wechatShare.getBroker_id())) {
            return VOUtil.genErrorResult(HSBCPSErrorCodes.WechatShareErr.NO_SUCH_WECHAT_SHARE, HSBCPSBackVO.class);
        }
        wechatShareService.delete(String.valueOf(param.getWechatshare_id()));
        
        return VOUtil.genSuccessResultVo();
    }
    
    @Override
    public HSBCPSBackVO funcWechatShareClick(WechatShareIdForm param) {
        Long broker_id = HSBCPSBizDataContext.getBrokerId();
        if (null == broker_id) {
            return VOUtil.genErrorResult(HSBCPSErrorCodes.ClientErr.NO_SUCH_CLIENT, HSBCPSBackVO.class);
        }
        WechatShare wechatShare = wechatShareService.get(param.getWechatshare_id());
        if (wechatShare == null || !broker_id.equals(wechatShare.getBroker_id())) {
            return VOUtil.genErrorResult(HSBCPSErrorCodes.WechatShareErr.NO_EQUAL_DAV, HSBCPSBackVO.class);
        }
        wechatShareService.addShareTimes(Long.valueOf(param.getWechatshare_id()));
        
        return VOUtil.genSuccessResultVo();
    }
    
    private void updateWechatShare(WechatShareUpdateForm param, Long broker_id) {
        WechatShare wechatShare = new WechatShare();
        wechatShare.setWechatshare_id(Long.valueOf(param.getWechatshare_id()));
        wechatShare.setShare_abstract(param.getShare_abstract());
        wechatShare.setShare_title(param.getShare_title());
        wechatShare.setLink_url(param.getLink_url());
        wechatShare.setImg_url(param.getImg_url());
        wechatShare.setQrcode_url(param.getQrcode_url());
        wechatShareService.update(wechatShare);
    }
}
